package com.huawei.hwid.api.common;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.cloudservice.b;
import com.huawei.cloudservice.b.a;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b.e;
import com.huawei.hwid.core.helper.handler.ErrorStatus;

public class h {
    private b a;
    private Context b = null;
    private Intent c = null;
    private int d = 0;
    private int e = 0;
    private CloudRequestHandler f = null;
    private boolean g = false;
    private ServiceConnection h = new ServiceConnection(this) {
        final /* synthetic */ h a;

        {
            this.a = r1;
        }

        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            boolean z = false;
            e.a("CloudAccountServiceClient2", "onServiceConnected---", true);
            int i = 0;
            while (i < 20) {
                this.a.a = a.a(iBinder);
                try {
                    if (this.a.a != null) {
                        break;
                    }
                    Thread.sleep(200);
                    i++;
                } catch (InterruptedException e) {
                    e.a("CloudAccountServiceClient2", "service cannot connected", true);
                }
            }
            String str = "CloudAccountServiceClient2";
            StringBuilder append = new StringBuilder().append("onServiceConnected---mICloudAccount=");
            if (this.a.a != null) {
                z = true;
            }
            e.a(str, append.append(z).toString(), true);
            this.a.c();
        }

        public void onServiceDisconnected(ComponentName componentName) {
            e.a("CloudAccountServiceClient2", "onServiceDisconnected", true);
            this.a.a = null;
        }
    };

    public h(Context context, Intent intent, int i, int i2) {
        this.b = context;
        this.c = intent;
        this.d = i;
        this.e = i2;
    }

    public void a() {
        e.a("CloudAccountServiceClient2", "bindService", true);
        if (this.f == null) {
            e.d("CloudAccountServiceClient2", "has not set mCloudRequesthandler", true);
        } else if (com.huawei.hwid.api.common.apkimpl.a.a(this.b)) {
            b();
        } else {
            this.f.onError(new ErrorStatus(31, "Account hasnot login"));
        }
    }

    private void b() {
        Intent intent = new Intent();
        intent.setAction("com.huawei.hwid.ICloudService");
        intent.setPackage("com.huawei.hwid");
        e.a("CloudAccountServiceClient2", "begin to bindService", true);
        this.g = this.b.getApplicationContext().bindService(intent, this.h, 1);
    }

    private void c() {
        e.a("CloudAccountServiceClient2", "saveLogoutIntent", true);
        if (this.a != null) {
            try {
                int a = this.a.a(this.c, this.d, this.e);
                if (3 == a) {
                    this.f.onError(new ErrorStatus(38, "already exists same priority"));
                } else {
                    Bundle bundle = new Bundle();
                    if (2 == a) {
                        bundle.putBoolean(HwAccountConstants.SERVICETOKENAUTH_IS_SECCUSS, true);
                    } else if (1 == a) {
                        bundle.putBoolean(HwAccountConstants.SERVICETOKENAUTH_IS_SECCUSS, false);
                    }
                    e.a("CloudAccountServiceClient2", "onFinish", true);
                    this.f.onFinish(bundle);
                }
                if (this.g) {
                    this.b.getApplicationContext().unbindService(this.h);
                    this.g = false;
                    return;
                }
                return;
            } catch (RemoteException e) {
                e.d("CloudAccountServiceClient2", "Call Remote Exception", true);
                this.f.onError(new ErrorStatus(38, "RemoteException"));
                return;
            }
        }
        this.f.onError(new ErrorStatus(38, "failed to get the remote interface"));
    }

    public void a(CloudRequestHandler cloudRequestHandler) {
        this.f = cloudRequestHandler;
    }
}
